//
//  WmcEditBrandVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/9.
//

import UIKit
import YPBedrock
import YPNetwork

// MARK: - 扩展WmcEditBrandVC + WmcNavigatorProtocol

extension WmcEditBrandVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.editbrand]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcEditBrandVM()
        if let viewModel = parms.context as? WmcEditBrandVM {
            vm = viewModel
        }
        let vc = WmcEditBrandVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcEditBrandVC + WmcVMProtocol

extension WmcEditBrandVC: WmcVMProtocol {
    typealias VMType = WmcEditBrandVM
}

// MARK: - [WmcEditBrandVC]

class WmcEditBrandVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "品牌图"
        
        viewModel.loadData()
    }

    // Internal

    lazy var saveBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "保存")
        return btn
    }()

    lazy var watermarkContentView: WmcWatermarkContentView = {
        let view = WmcWatermarkContentView()
        view.isUserInteractionEnabled = false
        return view
    }()

    lazy var toolView: WmcEditBrandToolView = {
        let view = WmcEditBrandToolView()
        return view
    }()

    override func makeUI() {
        view.backgroundColor = .wmc_grayH5_606066

        makeNavView()

        navView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-6)
            make.width.equalTo(52.wmc_autoHor)
            make.height.equalTo(32)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        addSubview(watermarkContentView)
        watermarkContentView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.5)
        }

        addSubview(toolView)
        toolView.snp.makeConstraints { make in
            make.top.equalTo(watermarkContentView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }

        reloadToolViewItem()
    }

    override func bindViewAction() {
        // 点击保存
        saveBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self,
                  let wm = weakSelf.viewModel.watermarkModel else { return }
            // 判断品牌图是否需要上传
            if let localWmcImage = wm.optionBrand()?.brand.localImage {
                WmcHudManager.loading()
                // 加载图片
                localWmcImage.loadUIImage { image in
                    guard let imageData = image.yp.pngData else { return }
                    let dateFolderName = Date().yp.string("yyyyMMdd")
                    let imagePath = "ios/\(dateFolderName)/\(UUID().uuidString).png"
                    YPNetworkUploadRequest.uploadTask(data: imageData, fileName: imagePath, success: { response in
                        WmcHudManager.dismiss()
                        // 上传完后删除本地图，保留Url
                        wm.optionBrand()?.brand.localImage = nil
                        wm.optionBrand()?.content = "https://staticscdn.zgzpsjz.com/\(imagePath)"
                        // 编辑完成
                        guard let weakSelf = self else { return }
                        weakSelf.backAction()
                        weakSelf.viewModel.editComplete?(wm)
                    }, failure: { error in
                        WmcHudManager.toast("网络不佳，请稍后再试")
                        WmcHudManager.dismiss()
                    })
                }
            } else {
                weakSelf.backAction()
                weakSelf.viewModel.editComplete?(wm)
            }
        }

        // 工具栏操作事件
        toolView.didSelectItem = { [weak self] type in
            guard let weakSelf = self,
                  let optionBrand = weakSelf.viewModel.watermarkModel?.optionBrand() else { return }
            let brand = optionBrand.brand
            switch type {
            case .brandLibrary: weakSelf.didSelectBrandLibrary(brand) // 品牌库
            case .albumSelect: weakSelf.didSelectBrandAlbumSelect(brand)
            case .tailor: weakSelf.didSelectBrandTailor(optionBrand, brand: brand) // 裁剪
            case .changeLocation: weakSelf.didSelectBrandChangeLocation(brand) // 修改品牌图位置
            case .removeBackgroundColor: weakSelf.didSelectBrandRemoveBackgroundColor(brand) // 移除底色
            case .scale: weakSelf.didSelectBrandScale(brand) // 调整缩放
            case .alpha: weakSelf.didSelectBrandAlpha(brand) // 调整透明
            }
        }
    }

    override func bindViewModel() {
        // 当水印模型发生更新
        viewModel.watermarkUpdateRelay.subscribe(onNext: { [weak self] value in
            guard let weakSelf = self else { return }
            // 是否需要认证水印
            let showCertification = !WmcPreferenceSettingsManager.shared.cameraSetting.hiddenOfficialMark
            weakSelf.watermarkContentView.setWatermarkModel(value,
                                                            styleMode: weakSelf.viewModel.watermarkStyleMode,
                                                            showCertification: showCertification)
        }).disposed(by: rx.disposeBag)
        
        
    }
}
